package com.example.anni.zhuzhoujia.xiaowan;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.anni.zhuzhoujia.R;

import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by Anni on 2016/6/12.
 */
public class RegisterActivity extends AppCompatActivity {
    private EditText etNickName;
    private EditText etEmail;
    private EditText etRegisterUserName;
    private EditText etRegisterPassword;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register_activity);
        etNickName = (EditText) findViewById(R.id.et_nickName);
        etEmail = (EditText) findViewById(R.id.et_email);
        etRegisterUserName = (EditText) findViewById(R.id.et_register_userName);
        etRegisterPassword = (EditText) findViewById(R.id.et_register_password);
        initToolbar();
    }

    private void initToolbar() {
        TextView tvNameTitle;
        tvNameTitle = (TextView) findViewById(R.id.tv_name_title);
        tvNameTitle.setText("注册");
    }

    public void initRegister(View v) {
        final RequestParams params =
                new RequestParams("http://tr.zzapi.gson.cn/api/user/register");
        params.addBodyParameter("nickName", etNickName.getText().toString());
        params.addBodyParameter("email", etEmail.getText().toString());
        params.addBodyParameter("userName", etRegisterUserName.getText().toString());
        params.addBodyParameter("password", etRegisterPassword.getText().toString());
        x.http().post(params, new CallBackAdapter<String>() {
            @Override
            public void onSuccess(String s) {
                //Toast.makeText(RegisterActivity.this, s, Toast.LENGTH_LONG).show();
                JSONObject json = JSON.parseObject(s);
                if (json.getBoolean("success")) {
                    SharedPreferences preferences =
                            PreferenceManager.getDefaultSharedPreferences(RegisterActivity.this);
                    SharedPreferences.Editor editor = preferences.edit();
                    editor.putString("etRegisterUserName", etRegisterUserName.getText().toString());
                    editor.putString("etRegisterPassword", etRegisterPassword.getText().toString());
                    editor.commit();
                    Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                    startActivityForResult(intent, 0x003);
                    Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_LONG).show();
                } else if (etRegisterUserName.getText().toString().equals("")) {
                    Toast.makeText(RegisterActivity.this,
                            "账号" + json.getJSONObject("data").getString("userName"), Toast.LENGTH_LONG).show();
                } else if (etRegisterPassword.getText().toString().equals("")) {
                    Toast.makeText(RegisterActivity.this,
                            "密码" + json.getJSONObject("data").getString("password"), Toast.LENGTH_LONG).show();
                } else {
                    // Toast.makeText(RegisterActivity.this, s, Toast.LENGTH_LONG).show();
                    Toast.makeText(RegisterActivity.this,
                            json.getString("message"), Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}

